package mahi.thread;

import java.util.LinkedList;
import java.util.Queue;


//wait
//notify
//notify all

public class BlockingQueue {
	private Queue<String> queue = new LinkedList<String>();
    private int capacity;

    public BlockingQueue(int capacity) {
        this.capacity = capacity;
    }

    public synchronized void put(String element) throws InterruptedException {
        while(queue.size() == capacity) {
            wait();
        }

        queue.add(element);
        notify();
    }

    public synchronized String take() throws InterruptedException {
        while(queue.isEmpty()) {
            wait();
        }

        String item = queue.remove();
        notify();
        return item;
    }
    
    
    public static void main(String[] args) throws InterruptedException {
		BlockingQueue blockingQueue = new BlockingQueue(3);
		blockingQueue.take();
		Thread.sleep(3000);
		blockingQueue.put("sdkhfnkdaff");
		
	}
}
